package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.crm.entity.GwcszT;
import cn.e4j.modular.crm.entity.GwdfT;
import cn.e4j.modular.crm.mapper.GwdfTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单变更 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-03-13
 */
@Service
public class GwdfTServiceImpl extends ServiceImpl<GwdfTMapper, GwdfT> {

    @Autowired
    private GwcsTServiceImpl gwcsTService;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdflist(Page<Object> page, Dto dto) {
//        List<Map<String, Object>> gwdfDtos = this.baseMapper.listGwdfPage(page, dto);
        List<Map<String, Object>> gwdfDtos = InvokeUtil.invokeBeanMethod("gwdfTMapper,listGwdfPage",page,dto);
        return gwdfDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCOPTCList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> tcDtos = this.baseMapper.getCOPTCList(page, dto);
        return tcDtos;
    }

    @Transactional(readOnly = true)
    public Dto getCOPTCInfo(String mlegal, String TC002) {
        Dto tcDtos = this.baseMapper.getCoptcInfo(mlegal, TC002);
        return tcDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCOPTDList(Page<Object> page, Dto dto) {
        List<Map<String, Object>> tdDtos = this.baseMapper.getCOPTDList(page, dto);
        return tdDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getCOPTDData(Dto dto) {
        List<Map<String, Object>> tdDtos = this.baseMapper.getCOPTDData(dto);
        return tdDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwdfInfo(Dto dto) {
        List<Map<String, Object>> gwdfDtos = this.baseMapper.getGwdfInfo(dto);
        return gwdfDtos;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getGwcsList(Dto dto) {
        List<Map<String, Object>> gwcsDtos = this.baseMapper.getGwcsList(dto);
        return gwcsDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwcs112(Dto dto) {
        Dto gwcs112 = this.baseMapper.getGwcs112(dto);
        return gwcs112;
    }

    @Transactional(readOnly = true)
    public Dto getGwak063(Dto dto) {
        Dto gwak063 = this.baseMapper.getGwak063(dto);
        return gwak063;
    }

    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto) {
        Date date = new Date();
        dto.put("GWDFDOCNO", "DDBD" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        return dto;
    }

    @Transactional(readOnly = true)
    public Dto getGwdf002(Dto dto) {
        Dto maxGwdf002 = this.baseMapper.getMaxGWDF002(dto);
        if (maxGwdf002 != null) {
            dto.put("GWDF002", maxGwdf002.getBigDecimal("GWDF002_MAX").add(new BigDecimal(1)));
        } else {
            dto.put("GWDF002", 1);
        }
        return dto;
    }

    @Transactional
    public void addData(List<GwdfT> gwdfTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        //增加明细
        Date date = new Date();
        String GWDFTIME = DateUtils.formatDate(date, "HH:mm:ss");
        GwcsT gwcsTQ = this.baseMapper.getMaxGwcsseq(gwdfTList.get(0).getGWDF001());
        int count = 1;
        if(gwcsTQ != null){
           count = gwcsTQ.getGWCSSEQ().intValue() + 1;
        }
        for (GwdfT gwdfT : gwdfTList) {
            gwdfT.setGWDFENT(shiroUser.getDBC());
            gwdfT.setGWDFACTI("N");
            if(gwdfT.getGWDF005() == null){
                gwdfT.setGWDF005(new BigDecimal(count));
                count ++;
            }
            gwdfT.setGWDFTIME(GWDFTIME);
            gwdfT.insert();
            if (!gwdfT.getGWDF004().equals("")) {
                GwcsT gwcsT = new GwcsT();
                gwcsT.setGWCSENT(shiroUser.getDBC());
                gwcsT.setGWCSDOCNO(gwdfT.getGWDF001());
                gwcsT.setGWCSSEQ(new BigDecimal(gwdfT.getGWDF004()));
                gwcsT.setGWCS113(gwdfT.getGWDF023());
                UpdateWrapper<GwcsT> gwcsTNewUpdateWrapper = new UpdateWrapper<>();
                gwcsTNewUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
                gwcsTNewUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
                gwcsTNewUpdateWrapper.eq("GWCSSEQ", gwcsT.getGWCSSEQ());
                gwcsT.update(gwcsTNewUpdateWrapper);
            }
        }
    }

    @Transactional
    public void editData(List<GwdfT> gwdfTList, String GWDFDOCNO) {
        ShiroUser shiroUser = ShiroKit.getUser();

        GwdfT gwdfT_del = new GwdfT();
        UpdateWrapper<GwdfT> gwddTUpdateWrapper = new UpdateWrapper<>();
        gwddTUpdateWrapper.eq("GWDFENT", shiroUser.getDBC());
        gwddTUpdateWrapper.eq("GWDFDOCNO", GWDFDOCNO);
        gwdfT_del.delete(gwddTUpdateWrapper);

        Date date = new Date();
        String GWDFTIME = DateUtils.formatDate(date, "HH:mm:ss");
        for (GwdfT gwdfT : gwdfTList) {
            gwdfT.setGWDFENT(shiroUser.getDBC());
            gwdfT.setGWDFACTI("N");
            gwdfT.setGWDFTIME(GWDFTIME);
            gwdfT.insert();
            if (!gwdfT.getGWDF004().equals("")) {
                GwcsT gwcsT = new GwcsT();
                gwcsT.setGWCSENT(shiroUser.getDBC());
                gwcsT.setGWCSDOCNO(gwdfT.getGWDF001());
                gwcsT.setGWCSSEQ(new BigDecimal(gwdfT.getGWDF004()));
                gwcsT.setGWCS113(gwdfT.getGWDF023());
                UpdateWrapper<GwcsT> gwcsTNewUpdateWrapper = new UpdateWrapper<>();
                gwcsTNewUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
                gwcsTNewUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
                gwcsTNewUpdateWrapper.eq("GWCSSEQ", gwcsT.getGWCSSEQ());
                gwcsT.update(gwcsTNewUpdateWrapper);
            }
        }
    }

    @Transactional
    public void delete(Dto dto) {
        GwdfT gwdfT = new GwdfT();
        UpdateWrapper<GwdfT> gwdfTUpdateWrapper = new UpdateWrapper<>();
        gwdfTUpdateWrapper.eq("GWDFENT", dto.getString("GWDFENT"));
        gwdfTUpdateWrapper.eq("GWDFDOCNO", dto.getString("GWDFDOCNO"));

        gwdfT.delete(gwdfTUpdateWrapper);
    }

    @Transactional
    public void delDataBatch(List<GwdfT> gwdfTList) {
        for (GwdfT gwdfT : gwdfTList) {
            UpdateWrapper<GwdfT> gwdfTUpdateWrapper = new UpdateWrapper<>();
            gwdfTUpdateWrapper.eq("GWDFENT", gwdfT.getGWDFENT());
            gwdfTUpdateWrapper.eq("GWDFDOCNO", gwdfT.getGWDFDOCNO());

            gwdfT.delete(gwdfTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    public List<GwdfT> getGwdfData(Dto dto) {
        List<GwdfT> gwdfTList = this.baseMapper.getGwdfData(dto);
        return gwdfTList;
    }

    @Transactional(readOnly = true)
    public Dto getSumTh008(Dto dto) {
        Dto sumTh008 = this.baseMapper.getSumTh008(dto);
        if (sumTh008 == null) {
            Dto sumTh008_1 = Dtos.newDto();
            sumTh008_1.put("TH008_SUM", 0);
            return sumTh008_1;
        }
        return sumTh008;
    }

    @Transactional
    public void updateGwdf015(List<GwdfT> gwdfTList, String GWDFDOCNO) {
        ShiroUser shiroUser = ShiroKit.getUser();

        for (GwdfT gwdfT : gwdfTList) {
            Dto dto = Dtos.newDto();
            dto.put("GWDFENT", shiroUser.getDBC());
            dto.put("GWDFDOCNO", GWDFDOCNO);
            dto.put("GWDF003", gwdfT.getGWDF003());
            dto.put("GWDF004", gwdfT.getGWDF004());
            dto.put("GWDF015", gwdfT.getGWDF015());
            this.baseMapper.updateGwdf1(dto);
//            UpdateWrapper<GwdfT> gwdfTUpdateWrapper = new UpdateWrapper<>();
//            gwdfTUpdateWrapper.eq("GWDFENT", shiroUser.getDBC());
//            gwdfTUpdateWrapper.eq("GWDFDOCNO", GWDFDOCNO);
//            gwdfTUpdateWrapper.eq("GWDF003", gwdfT.getGWDF003());
//            gwdfTUpdateWrapper.eq("GWDF004", gwdfT.getGWDF004());
//            gwdfT.update(gwdfTUpdateWrapper);
        }
    }

    @Transactional
    public void updateCOPTC_D(Dto dto) {
        this.baseMapper.updateCOPTD(dto);
        this.baseMapper.updateCOPTC(dto);
    }

    @Transactional
    public void updateGWCS_GWCSZ(GwcsT gwcsT, GwcszT gwcszT, GwcsT gwcsTNew) {
        if (gwcsT.getGWCSSEQ() != null) {
            UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
            gwcsTUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
            gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
            gwcsTUpdateWrapper.eq("GWCSSEQ", gwcsT.getGWCSSEQ());
            gwcsT.update(gwcsTUpdateWrapper);
        }

        UpdateWrapper<GwcsT> gwcsTNewUpdateWrapper = new UpdateWrapper<>();
        gwcsTNewUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
        gwcsTNewUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
        gwcsTNew.update(gwcsTNewUpdateWrapper);

        if (gwcszT.getGWCSZSEQ() != null) {
            UpdateWrapper<GwcszT> gwcszTUpdateWrapper = new UpdateWrapper<>();
            gwcszTUpdateWrapper.eq("GWCSZENT", gwcszT.getGWCSZENT());
            gwcszTUpdateWrapper.eq("GWCSZDOCNO", gwcszT.getGWCSZDOCNO());
            gwcszTUpdateWrapper.eq("GWCSZSEQ", gwcszT.getGWCSZSEQ());
            gwcszT.update(gwcszTUpdateWrapper);
        }
    }

    @Transactional
    public void updateGwdf(Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwdfT gwdfT = new GwdfT();
        gwdfT.setGWDFACTI("Y");
        gwdfT.setGWDF033(shiroUser.getACCOUNT());
        gwdfT.setGWDF034(LocalDate.now());
        dto.put("GWDF034", LocalDate.now());
        dto.put("GWDF033", shiroUser.getACCOUNT());
        dto.put("GWDFACTI", "Y");
        this.baseMapper.updateGwdf(dto);
//        UpdateWrapper<GwdfT> gwdfTUpdateWrapper = new UpdateWrapper<>();
//        gwdfTUpdateWrapper.eq("GWDFENT", dto.getString("ENT"));
//        gwdfTUpdateWrapper.eq("GWDFDOCNO", dto.getString("GWDFDOCNO"));
//        gwdfT.update(gwdfTUpdateWrapper);
    }

    @Transactional(readOnly = true)
    public Dto getMaxTD003(String mlegal, String TD002) {
        Dto tdDtos = this.baseMapper.getMaxTD003(mlegal, TD002);
        return tdDtos;
    }

    @Transactional(readOnly = true)
    public Dto getGwam(Dto dto) {
        Dto gwamDtos = this.baseMapper.getGwam(dto);
        return gwamDtos;
    }

    @Transactional
    public void insertCOPTD(Dto maxTD003, GwdfT gwdfT, Dto gwam, BigDecimal gwcsseq) {
        BigDecimal TD003 = maxTD003.getBigDecimal("TD003_MAX").add(new BigDecimal(1));
        DecimalFormat df = new DecimalFormat("0000");
        String td003 = df.format(TD003);
        Dto COPTD = Dtos.newDto();
        COPTD.put("MB001", gwdfT.getGWDFMLEGAL());//目标数据库
        COPTD.put("TD001", "FXDD");
        COPTD.put("TD002", gwdfT.getGWDF003());
        COPTD.put("TD003", td003);
        COPTD.put("TD004", gwdfT.getGWDF011());//品号
        if (gwam != null) {
            COPTD.put("TD005", gwam.getString("GWAML003"));
            COPTD.put("TD006", gwam.getString("GWAML004"));
        }
        //COPTD.put("TD007", gwdfT.getGWDF023());//仓库
        //COPTD.put("TD008", gwdfT.getGWDF014());//数量
        COPTD.put("TD009", 0);
        COPTD.put("TD010", gwdfT.getGWDF012());//单位
        //COPTD.put("TD011", gwdfT.getGWDF017());//单价
        //COPTD.put("TD012", gwdfT.getGWDF026());//金额
        //COPTD.put("TDI03", gwdfT.getGWDF026().multiply(maxTD003.getBigDecimal("TC009")));//
        COPTD.put("TD013", maxTD003.getString("TD013"));
        COPTD.put("TD016", "N");
        COPTD.put("TD021", "Y");
        //COPTD.put("TD026", gwdfT.getGWDF036());
        Dto mbDto = Dtos.newDto();
        mbDto.put("COMPANY", gwdfT.getGWDFMLEGAL());
        mbDto.put("MB001", gwdfT.getGWDF011());
        Dto mb = this.gwcsTService.getMB(mbDto);
        if (mb != null) {
            COPTD.put("TD036", mb.getString("MB090"));//包装单位
            mbDto.put("TD036", mb.getString("MB090"));
            Dto tDto = this.gwcsTService.getTD032(mbDto);
            if (mb != null && tDto != null) {
                //COPTD.put("TD032", gwdfT.getGWDF014().multiply(tDto.getBigDecimal("TD032")));//包装数量
                COPTD.put("TD032", tDto.getBigDecimal("TD032"));//包装数量
            } else {
                COPTD.put("TD032", 0);
            }
        } else {
            COPTD.put("TD032", 0);
        }
        COPTD.put("TD037", gwdfT.getGWDF010());//税率
        //COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP)));
        //COPTD.put("TDI01", (gwdfT.getGWDF026().subtract(gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP))).multiply(maxTD003.getBigDecimal("TC009")));//gwcs036-gwcs040
        //COPTD.put("TD039", gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP));
        COPTD.put("TDI02", (gwdfT.getGWDF029().setScale(2, RoundingMode.HALF_UP)).multiply(maxTD003.getBigDecimal("TC009")));
        COPTD.put("TD048", gwcsseq);
        COPTD.put("TD049", gwdfT.getGWDF001());
        COPTD.put("TD062", "N");
        COPTD.put("TDD01", "N");
        COPTD.put("UDF07", COPTD.getString("TD004"));
        COPTD.put("UDF61", 0);
        COPTD.put("COMPANY", gwdfT.getGWDFMLEGAL());
        COPTD.put("FLAG", 1);

        //修改ERP单身数据
        //Dto COPTC = Dtos.newDto();
        COPTD.put("mlegal", gwdfT.getGWDFMLEGAL());
        COPTD.put("TC038", gwdfT.getGWDF031());//备注
        COPTD.put("UDF12", gwdfT.getGWDF007());//返利类型
        COPTD.put("TD008", gwdfT.getGWDF014());//订单数量
        COPTD.put("TD007", gwdfT.getGWDF023());//仓库
        COPTD.put("TD011", gwdfT.getGWDF016());//价格
        COPTD.put("TD012", gwdfT.getGWDF026());//金额
        COPTD.put("TD039", gwdfT.getGWDF029());//税额
        COPTD.put("TD038", gwdfT.getGWDF026().subtract(gwdfT.getGWDF029()));//税前金额
        COPTD.put("TDI01", COPTD.getBigDecimal("TD038").multiply(gwdfT.getGWDF024()));//本币税前金额
        COPTD.put("TDI02", gwdfT.getGWDF029().multiply(gwdfT.getGWDF024()));//本币税前金额
        COPTD.put("TDI03", gwdfT.getGWDF026().multiply(gwdfT.getGWDF024()));//本币金额
        //COPTC.put("TD002",gwdfT.getGWDF003());//订单号
        //COPTC.put("TD003",gwdfT.getGWDF004());//订单项次
        COPTD.put("TD026", gwdfT.getGWDF036());//折扣率

        this.baseMapper.insertCOPTD(COPTD);
        this.baseMapper.updateCOPTC(COPTD);
    }

    @Transactional(readOnly = true)
    public GwcsT getMaxGwcsseq(String GWCSDOCNO) {
        GwcsT gwcsDtos = this.baseMapper.getMaxGwcsseq(GWCSDOCNO);
        return gwcsDtos;
    }

    @Transactional
    public void addGwcs(GwcsT gwcsT, GwdfT gwdfT) {
        String GWCSTIME = gwcsT.getGWCSTIME();
        LocalDate GWCSDATE = gwcsT.getGWCSDATE();
        BigDecimal GWCSSEQ = gwcsT.getGWCSSEQ();
        gwcsT.setGWCSSEQ(GWCSSEQ.add(new BigDecimal(1)));
        gwcsT.setGWCS001(gwdfT.getGWDF011());
        gwcsT.setGWCS002(gwdfT.getGWDF012());
        gwcsT.setGWCS003(gwdfT.getGWDF014());
        gwcsT.setGWCS004(gwdfT.getGWDF017());
        gwcsT.setGWCS005(gwdfT.getGWDF026());
        gwcsT.setGWCS112(gwdfT.getGWDF021());
        gwcsT.setGWCS113(gwdfT.getGWDF022());
        gwcsT.setGWCS115(gwdfT.getGWDF016());
        gwcsT.setGWCS117(gwdfT.getGWDF007());
        gwcsT.setGWCS119("1");
        gwcsT.setGWCS050(gwdfT.getGWDF031());//备注
        gwcsT.setGWCS037(gwdfT.getGWDF017().multiply(gwcsT.getGWCS034()));//本币单价
        gwcsT.setGWCS038(gwdfT.getGWDF026().multiply(gwcsT.getGWCS034()));//本币金额
        gwcsT.setGWCS040(gwdfT.getGWDF029());//原币税额
        gwcsT.setGWCS041(gwdfT.getGWDF030());//本币税额

        gwcsT.insert();

        GwcsT gwcsTNew = new GwcsT();
        gwcsTNew.setGWCSTIME(GWCSTIME);
        gwcsTNew.setGWCSDATE(GWCSDATE);
        UpdateWrapper<GwcsT> gwcsTNewUpdateWrapper = new UpdateWrapper<>();
        gwcsTNewUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
        gwcsTNewUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
        gwcsTNewUpdateWrapper.eq("GWCSSEQ", gwcsT.getGWCSSEQ());
        gwcsTNew.update(gwcsTNewUpdateWrapper);
    }

    @Transactional
    public void delGwcs(GwcsT gwcsT) {
        UpdateWrapper<GwcsT> gwcsTUpdateWrapper = new UpdateWrapper<>();
        gwcsTUpdateWrapper.eq("GWCSENT", gwcsT.getGWCSENT());
        gwcsTUpdateWrapper.eq("GWCSDOCNO", gwcsT.getGWCSDOCNO());
        gwcsTUpdateWrapper.eq("GWCSSEQ", gwcsT.getGWCSSEQ());
        gwcsT.delete(gwcsTUpdateWrapper);
    }

    @Transactional
    public void delCOPTD(Dto delCoptc) {
        this.baseMapper.delCOPTD(delCoptc);
        this.baseMapper.updateCOPTCDEL(delCoptc);
    }

    @Transactional
    public void update(List<GwdfT> gwdfTList) {
        for (GwdfT gwdf : gwdfTList) {
            Dto dto = Dtos.newDto();
            dto.put("TD002", gwdf.getGWDF003());
            dto.put("TD003", gwdf.getGWDF004());
            dto.put("TD007", gwdf.getGWDF023());
            dto.put("mlegal", gwdf.getGWDFMLEGAL());
            this.baseMapper.updateTD(dto);
        }
    }

    @Transactional
    public void update2(List<GwdfT> gwdfTList) {
        for (GwdfT gwdf : gwdfTList) {
            Dto dto = Dtos.newDto();
            dto.put("TD002", gwdf.getGWDF003());
            dto.put("TD003", gwdf.getGWDF004());
            dto.put("TD007", gwdf.getGWDF022());
            dto.put("mlegal", gwdf.getGWDFMLEGAL());
            this.baseMapper.updateTD(dto);
        }
    }
}
